package com.zyf.lj.controller;

import com.zyf.lj.entity.CollegeInfo;
import com.zyf.lj.service.CollegeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/college")
public class CollegeController {
    @Autowired
    private CollegeInfoService collegeInfoService;

    @GetMapping("/info")
    public List<Map<String, Object>> getCollegeInfo(@RequestParam(required = false) String type) {
        List<CollegeInfo> list = (type != null && !type.isEmpty())
            ? collegeInfoService.getCollegeInfoByType(type)
            : collegeInfoService.getAllCollegeInfo();
            
        return list.stream().map(info -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", info.getId());
            map.put("type", info.getType());
            // 确保 title 不为 null
            map.put("title", info.getTitle() != null ? info.getTitle() : "");
            // 确保 content 不为 null
            map.put("content", info.getContent() != null ? info.getContent() : "");
            
            // 处理图片数组
            List<String> images;
            if (info.getImages() == null || info.getImages().trim().isEmpty()) {
                images = Collections.emptyList();
            } else {
                images = Arrays.stream(info.getImages().split(","))
                    .map(String::trim)
                    .filter(s -> !s.isEmpty())
                    .collect(Collectors.toList());
            }
            map.put("images", images);
            
            return map;
        }).collect(Collectors.toList());
    }
}